package com.company.thread;


import jdk.nashorn.internal.ir.Block;
import lombok.extern.slf4j.Slf4j;

// wait-notify通知机制
public class Thread19 {
    private static boolean hasRes1 = false;
    private static boolean hasRes2 = false;
    private static final Object lock = new Object();
    public static void main(String[] args) {

        // 线程t1需要资源res1才能继续进行工作，没有获取到res则阻塞
        new Thread(()->{
            synchronized (lock) {
                System.out.println("是否有资源1"+hasRes1);
                while (!hasRes1) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("我有了资源1，我可以开始工作了");
            }

        },"t1").start();


        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        new Thread(()->{
            synchronized (lock) {
                hasRes1 = true;
                System.out.println("资源1已经就位");
                lock.notifyAll();
            }

        },"t2").start();
    }







}
